import 'package:flutter/material.dart';
import 'package:travel/chat/screenutil/screenutil.dart';

/// 内容为 音频 的组件盒子
class AudioBox extends StatefulWidget {
  /// 音频时长
  final int audioTimelength;

  /// 播放状态
  final bool audioPlayStatus;

  const AudioBox({
    super.key,
    this.audioTimelength = 0,
    this.audioPlayStatus = false,
  });

  @override
  State<AudioBox> createState() => _AudioBoxState();
}

class _AudioBoxState extends State<AudioBox> {
  @override
  Widget build(BuildContext context) {
    /// 图标切换状态
    bool iconSwitchStatus = false;

    /// 根据音频播放状态修改图标
    Widget audioFrequencyStatusWidget({Function? callback}) {
      Future.delayed(const Duration(seconds: 1), () {
        if (widget.audioPlayStatus && callback != null) {
          callback();
        }
      });

      if (!widget.audioPlayStatus && iconSwitchStatus) {
        iconSwitchStatus = false;
      }

      Icon icon = Icon(iconSwitchStatus ? Icons.volume_down : Icons.volume_up,
          size: sf(20), color: const Color(0xff1989fa));

      return icon;
    }

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          width: sz(20),
          height: sz(20),
          child: StatefulBuilder(builder: ((context, setState) {
            return audioFrequencyStatusWidget(callback: () {
              iconSwitchStatus = !iconSwitchStatus;

              setState(() {});
            });
          })),
        ),
        // 间隙盒子
        SizedBox(
          width: sw(5),
        ),
        // 图标
        Icon(Icons.multitrack_audio_outlined,
            size: sf(25), color: const Color(0xff1989fa)),
        Transform.translate(
          offset: Offset(sw(-8), 0),
          child: Icon(Icons.multitrack_audio_rounded,
              size: sf(25), color: const Color(0xff1989fa)),
        ),
        // 时长
        if (widget.audioTimelength > 0)
          Container(
            constraints: BoxConstraints(maxWidth: sw(75)),
            child: Text(
              "'${widget.audioTimelength}",
              style: TextStyle(fontSize: sf(15), color: Colors.black),
            ),
          ),
        //
      ],
    );
  }
}
